﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class PurchaseStorageUpdateHandler : IRequestHandler<PurchaseStorageUpdateCommand, ResultsDTO>
    {
        private readonly IBateRepository<PurchaseStorage> purchase;
        private readonly ILogger<PurchaseStorageUpdateHandler> logger;
        public PurchaseStorageUpdateHandler(IBateRepository<PurchaseStorage> purchase, ILogger<PurchaseStorageUpdateHandler> logger)
        {
            this.purchase = purchase;
            this.logger = logger;
        }
        /// <summary>
        /// 采购入库单修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async  Task<ResultsDTO> Handle(PurchaseStorageUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                var list = new ResultsDTO() { Code = 0 };
                var gh = new PurchaseStorage();
                var str = "UPDATE purchasestorage SET PurchaseNo='" + request.PurchaseNo + "', PurchaseName='" + request.PurchaseName + "',PurchaseDate='" + request.PurchaseDate + "',ArrivalNoticeId='" + request.ArrivalNoticeId + "',PurchaseIndentId='" + request.PurchaseIndentId + "',SupplierId='" + request.SupplierId + "',Remark='" + request.Remark + "'WHERE PurchaseId=" + request.PurchaseId + "";
                list.Code = await purchase.Add(str, gh, helper.MangoMesWrite);
                logger.LogInformation ("成功");
                return list;
            }
            catch (Exception ex)
            {
                logger.LogError("失败" + ex);
                throw;
            }
         
        }
    }
}
